package com.book.dao.reader.impl;

import com.book.dao.reader.ReaderDao;
import com.book.utils.DbUtils;
import com.book.pojo.Reader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ReaderDaoImpl implements ReaderDao {
    @Override
    public Reader getReaderByUsername(String username) {
        String sql = "SELECT * FROM reader WHERE username = ?";
        try (Connection conn = DbUtils.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, username);
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                return new Reader(
                        rs.getInt("id"),
                        rs.getString("name"),
                        rs.getString("username"),
                        rs.getString("password")
                );
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int addReader(Reader reader) {
        String sql = "INSERT INTO reader (name, username, password) VALUES (?, ?, ?)";
        try (Connection conn = DbUtils.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, reader.getName());
            pstmt.setString(2, reader.getUsername());
            pstmt.setString(3, reader.getPassword());
            return pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<Reader> getAllReaders() {
        List<Reader> readerList = new ArrayList<>();
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        try {
            conn = DbUtils.getConnection(); // 获取数据库连接
            String sql = "SELECT id, name, username, password FROM reader"; // 查询所有读者
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();

            while (rs.next()) {
                Reader reader = new Reader();
                reader.setId(rs.getInt("id"));
                reader.setName(rs.getString("name"));
                reader.setUsername(rs.getString("username"));
                reader.setPassword(rs.getString("password"));
                readerList.add(reader);
            }
            System.out.println("查询所有读者成功，返回列表！");
        } catch (SQLException e) {
            System.err.println("查询所有读者失败：" + e.getMessage());
            e.printStackTrace();
        } finally {
            DbUtils.close(rs, pstmt, conn); // 关闭资源
        }

        return readerList;
    }

    @Override
    public int deleteReader(int readerId) {
        System.out.println("正在删除读者，ID：" + readerId);

        int result = 0; // 默认删除失败

        try (Connection conn = DbUtils.getConnection();
             PreparedStatement pstmt = conn.prepareStatement("DELETE FROM reader WHERE id = ?")) {

            pstmt.setInt(1, readerId);
            result = pstmt.executeUpdate();

            if (result > 0) {
                System.out.println("读者删除成功，ID：" + readerId);
            } else {
                System.out.println("删除失败，可能该读者 ID 不存在：" + readerId);
            }
        } catch (SQLException e) {
            System.err.println("删除读者过程中出现 SQL 错误：" + e.getMessage());
            e.printStackTrace();
        }

        return result; // 返回受影响的行数，大于 0 代表删除成功
    }


}
